package src.leetcode.hot.mid_0199;

import src.tree.TreeNode;

import java.util.*;

public class rightSideView {
    public static void main(String[] args) {
        Solution solution = new Solution();
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,null,5,null,4));
        TreeNode root = TreeNode.createTree(list);
        System.out.println(solution.rightSideView(root));
    }
}

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        Deque<TreeNode> que = new LinkedList<>();
        List<Integer> list = new LinkedList<>();
        que.addFirst(root);
        while (!que.isEmpty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = que.pollLast();
                if (i + 1 == size) {
                    list.add(node.val);
                }
                if (node.left != null) que.addFirst(node.left);
                if (node.right != null) que.addFirst(node.right);
            }
        }
        return list;
    }
}